package com.study.repository;

import com.study.entity.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname LocationRepository
 * @Version 1.0.0
 * @Date 2022/4/2 10:49
 * @Created by yd
 * JpaRepository 封装了基本CRUD操作
 * JpaSpecificationExecutor 封装了复杂查询(分页)
 */
@Repository
public interface UsersRepository extends JpaRepository<Users, Long>, JpaSpecificationExecutor<Users> {

	List<Users> getLocationsByType(String type);
	//声明式查询 不提示了

	/**
	 * Jpql语句
	 *
	 * @Query(value = "from User where id = :id")
	 * User queryById(int id);
	 * <p>
	 * (2)?+参数下标，从1开始
	 * @Query(value = "from User where id = ?1")
	 * User queryById2(int id);
	 */
	@Query(value = "from Users where name = :name")
	Users findJpql(String name);

	/**
	 * jpql : from Users where name =:name and email =:email
	 */
	@Query(value = "from Users where name = :name and email =:email ")
	Users findJpqlUsersNameEmail(String name, String email);

	/**
	 * jpql 更新操作
	 * 需要配置 Modifying 和 Transactional
	 */
	@Transactional
	@Query(value = "update Users set name=:name where id=:id")
	@Modifying
	void updateJpqlUser(Long id, String name);

	/********************SQL查询*********************/
	/**
	 * nativeQuery=true 表示SQL查询
	 * nativeQuery=false 表示JPQL查询
	 */
	@Query(value = "select * from tuser", nativeQuery = true)
	List<Users> findSQL();

	@Query(value = "select * from tuser where name=:name and email=:email", nativeQuery = true)
	Users findSqlUser(String name, String email);

	/********************声明式查询*********************/
	/**
	 * 声明式查询
	 * <p>
	 * findBy 属性名称 + 查询方式
	 *
	 * @param name  名称
	 * @param email 邮箱
	 * @return 查询结果
	 */
	Users findByNameAndEmail(String name, String email);


	/**
	 * <strong>Like</strong>
	 * where users0_.name like ? escape ?
	 */
	List<Users> findByNameContaining(String name);

}
